package jp.co.showbiz.admin.report.imp;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;

import jp.co.showbiz.admin.report.dao.ReportDao;
import jp.co.showbiz.common.base.BaseException;
import jp.co.showbiz.common.entity.FileBean;
import jp.co.showbiz.common.util.CommUtil;

public class DigestServiceImpl {
	private ReportDao dao;
	private HashMap<String, String> globeMap;
	public DigestServiceImpl() throws BaseException, IOException {
		dao = new ReportDao();
	}
	public DigestServiceImpl(HashMap<String, String> globeMap) throws BaseException, IOException {
		dao = new ReportDao();
		this.globeMap = globeMap;
	}
	public List<Hashtable> getDigestImageInfo(DigestDto dto) throws SQLException {
		return dao.getDigestImageInfo(dto);
	}
	public ContentInfoDto getDigestImageInfo(int content_id) throws SQLException {
		return dao.getDigestImageInfo(content_id);
	}
	public void updadateDigestImageInfo(ContentInfoDto cDto) throws Exception {
		//イメージコピー
		imageCopy(cDto);
		//データベース更新
		dao.updateDigestImageInfo(cDto);
	}
	
	private void imageCopy(ContentInfoDto cDto) throws Exception {
		FileBean thumbVga = cDto.getThumb_vga_file();
		FileBean thumbQvga = cDto.getThumb_qvga_file();
		FileBean middleVga = cDto.getDetail_vga_file();
		FileBean middleQvga = cDto.getDetail_qvga_file();
		FileBean originalVga = cDto.getOriginal_vga_file();
		FileBean originalQvga = cDto.getOriginal_qvga_file();
		FileBean original2Vga = cDto.getOriginal2_vga_file();
		FileBean original2Qvga = cDto.getOriginal2_qvga_file();
		if (thumbVga != null && thumbQvga != null 
				&& middleVga != null && middleQvga != null 
				&& originalVga != null && originalQvga != null 
				&& original2Vga != null && original2Qvga != null) {
			String contextPath = globeMap.get("globe.path.base").concat(cDto.getFile_path());
			File dir = new File(contextPath);
			if (!dir.exists()) {
				dir.mkdirs();
			}
			
			String newFilePath = getExtensionFromFileName(thumbVga.getFileFileName(), cDto.getThumb_vga_fileName());
			cDto.setThumb_vga_fileName(newFilePath);
			File newFile = new File(contextPath.concat(newFilePath));
			CommUtil.copyFile(thumbVga.getFile(), newFile, true);

			newFilePath = getExtensionFromFileName(thumbQvga.getFileFileName(), cDto.getThumb_qvga_fileName());
			cDto.setThumb_qvga_fileName(newFilePath);
			newFile = new File(contextPath.concat(newFilePath));
			CommUtil.copyFile(thumbQvga.getFile(), newFile, true);
			
			newFilePath = getExtensionFromFileName(middleVga.getFileFileName(), cDto.getDetail_vga_fileName());
			cDto.setDetail_vga_fileName(newFilePath);
			newFile = new File(contextPath.concat(newFilePath));
			CommUtil.copyFile(middleVga.getFile(), newFile, true);
			
			newFilePath = getExtensionFromFileName(middleQvga.getFileFileName(), cDto.getDetail_qvga_fileName());
			cDto.setDetail_qvga_fileName(newFilePath);
			newFile = new File(contextPath.concat(newFilePath));
			CommUtil.copyFile(middleQvga.getFile(), newFile, true);
			
			newFilePath = getExtensionFromFileName(originalVga.getFileFileName(), cDto.getOriginal_vga_fileName());
			cDto.setOriginal_vga_fileName(newFilePath);
			newFile = new File(contextPath.concat(newFilePath));
			CommUtil.copyFile(originalVga.getFile(), newFile, true);
			
			newFilePath = getExtensionFromFileName(originalQvga.getFileFileName(), cDto.getOriginal_qvga_fileName());
			cDto.setOriginal_qvga_fileName(newFilePath);
			newFile = new File(contextPath.concat(newFilePath));
			CommUtil.copyFile(originalQvga.getFile(), newFile, true);
			
			newFilePath = getExtensionFromFileName(original2Vga.getFileFileName(), cDto.getOriginal2_vga_fileName());
			cDto.setOriginal2_vga_fileName(newFilePath);
			newFile = new File(contextPath.concat(newFilePath));
			CommUtil.copyFile(original2Vga.getFile(), newFile, true);
			
			newFilePath = getExtensionFromFileName(original2Qvga.getFileFileName(), cDto.getOriginal2_qvga_fileName());
			cDto.setOriginal2_qvga_fileName(newFilePath);
			newFile = new File(contextPath.concat(newFilePath));
			CommUtil.copyFile(original2Qvga.getFile(), newFile, true);
		}
	}
	
	private String getExtensionFromFileName(String name, String oldFile) {
		oldFile = oldFile.substring(0, oldFile.lastIndexOf("."));
    	return oldFile.concat((name.substring(name.lastIndexOf("."), name.length())).toLowerCase());
    }
	
	public void deleteContent(ContentInfoDto cDto) throws SQLException {
		dao.deleteContent(cDto);
	}
}
